//
//  DiaryEditWeatherTableViewCell.swift
//  MoodDiary
//
//  Created by MoodDiary on 2020/12/8.
//

import UIKit

protocol DiaryEditWeatherTableViewCellDeleagte: class {
    func diaryEditWeather(weather: String)
}

@objcMembers
class DiaryEditWeatherTableViewCell: UITableViewCell {

    @IBOutlet weak var collectionView: UICollectionView!
    
    let weatherArray = ["晴天", "多云", "阴天", "雨", "雪", "雾", "浮尘", "沙尘暴", "夜_晴", "夜_多云","夜_雾霾","夜_下雨","夜_下雪","夜_打雷","白_多云雷","白_多云有风","白_雷雨","夜_多云有风","夜_风","夜_雷雨","白_日落","白_有风"]
    
    var selectWeather: String? {
        didSet {
            selectIndex = weatherArray.firstIndex(of: selectWeather ?? "")
            
            DispatchQueue.main.async {
                
                if (self.selectIndex ?? 0) > 0 {
                    self.collectionView.scrollToItem(at: IndexPath(item: (self.selectIndex ?? 1) - 1, section: 0), at: .left, animated: false)
                }
                
                
            }
            
        }
    }
    
    var selectIndex: Int?
    weak var delegate: DiaryEditWeatherTableViewCellDeleagte?
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.sectionInset = UIEdgeInsets(top: 0, left: 25.0, bottom: 0, right: 25.0)
        flowLayout.scrollDirection = .horizontal
        collectionView.collectionViewLayout = flowLayout
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(UINib(nibName: "AddWeatherCollectionViewCell", bundle: .main), forCellWithReuseIdentifier: "AddWeatherCollectionViewCell")
    }

}

extension DiaryEditWeatherTableViewCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let margin = 25.0
        let itemLength = (Double(collectionView.width) - margin * 4) / 3 - 5.0
        
        return CGSize(width: itemLength, height: itemLength)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat { 25.0 }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { 25.0 }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { weatherArray.count }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: AddWeatherCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: "AddWeatherCollectionViewCell", for: indexPath) as! AddWeatherCollectionViewCell
        cell.imgV.image = UIImage(named: weatherArray[indexPath.row])
        
        if selectIndex == indexPath.row {
            cell.isSelectCell = true
        } else {
            cell.isSelectCell = false
        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        for i in 0..<collectionView.numberOfItems(inSection: 0) {
            if i == indexPath.row {
                selectIndex = i
                break
            }
        }
        
        delegate?.diaryEditWeather(weather: weatherArray[indexPath.row])
        
        collectionView.reloadData()
    }
    
}
